package com.qingke.beSuper2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.codec.digest.DigestUtils;

import com.qingke.beSuper2.pojo.Player;
import com.qingke.beSuper2.utils.PropertyManager;

public class BeSuperDao {

	public BeSuperDao() {
	}
	
	public Player login(String username, String password) {
		
		String sql = "select id, name, score from besuper.player where username = ? and password = ?";
		
		Connection conn = null;
		PreparedStatement stmt= null;
		ResultSet rs = null;
		
		try {
			conn = getConnection();
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, username);
			stmt.setString(2, DigestUtils.md5Hex(password));
			
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				Player player = new Player();
				long id = rs.getLong("id");
				String name = rs.getString("name");
				int score = rs.getInt("score");
				player.setId(id);
				player.setName(name);
				player.setScore(score);
				
				return player;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			clean(conn, stmt, rs);
		}
		return null;
	}
	
	public Player signup(String username, String password, String name) {
		
		String sql = "insert into besuper.player(`name`, `username`, `password`) values (?, ?, ?)";
		
		Connection conn = null;
		PreparedStatement stmt = null;
		
		try {
			conn = getConnection();
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, name);
			stmt.setString(2, username);
			stmt.setString(3, DigestUtils.md5Hex(password));
			
			int affectRows = stmt.executeUpdate();
			
			if (affectRows > 0) {
				return login(username, password);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			clean(conn, stmt, null);
		}
		
		return null;
	}
	
	public void searchQuestions() {
		
	}
	
	public void insertQuestion() {
		
	}
	
	public void updateQuestion() {
		
	}
	
	public void searchAnswers() {
		
	}
	
	public void insertAnswers() {
		
	}
	
	public void updateAnswer() {
		
	}
	
	public void acceptAnswer() {
		
	}
	
	private Connection getConnection() {
		Connection conn = null;
		String url = getProperty("url");
		String user = getProperty("user");
		String password = getProperty("password");
		try {
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	private String getProperty(String key) {
		PropertyManager pm = PropertyManager.getInstance("conn.properties");
		return pm.getProperty(key);
	}
	
	private void clean(Connection conn, Statement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
